package com.yingda.electronicscaleadvices;

import android.app.Application;
import android.content.Context;

import androidx.multidex.MultiDex;

import com.hjq.toast.ToastUtils;
import com.tencent.bugly.crashreport.CrashReport;
import com.yingda.electronicscaleadvices.greendao.DaoMaster;
import com.yingda.electronicscaleadvices.greendao.DaoSession;
import com.yingda.electronicscaleadvices.net.Common;
import com.yingda.electronicscaleadvices.net.ServerReleaseAddress;
import com.yingda.electronicscaleadvices.net.ServerTestAddress;
import com.yingda.electronicscaleadvices.utils.DbOpenHelper;
import com.yingda.electronicscaleadvices.utils.QZXTools;

import org.greenrobot.greendao.database.Database;

import java.security.Security;


public class MyAPP extends Application {
    public static final String TAG = "MyAPP";

    @Override
    protected void attachBaseContext(Context context) {
        super.attachBaseContext(context);
        MultiDex.install(this);
    }

    private static MyAPP myApplication;

    private DaoSession daoSession;



    public DaoSession getDaoSession() {
        return daoSession;
    }

    public static MyAPP getInstance() {
        return myApplication;
    }
    private static final String DATABASE_NAME = "greendao.db";
    @Override
    public void onCreate() {
        super.onCreate();
        /**
         * 引入bugly
         * bugly主动上报：
         * CrashReport.postCatchedException(thr);  // bugly会将这个throwable上报
         * */
        myApplication = this;
        CrashReport.initCrashReport(getApplicationContext(), "3d0135fa72", true);
        ToastUtils.init(this);
        //关闭日志
        //QZXTools.openLog=false;
        //开启日志
        QZXTools.openLog = true;
        //初始化数据库
        initGreenDAO();
        iniAddress();



    }
    private void iniAddress() {
        int type =  BuildConfig.ENV_TYPE;
        if (type == 3){
            Common.initAddress(ServerReleaseAddress.BaseUrl);
        }else {
            //测试环境
            Common.initAddress(ServerTestAddress.BaseUrl);
         // Common.initAddress(ServerReleaseAddress.BaseUrl);
        }

    }


    /**
     * 初始化greendao
     * <p>
     * 解释一下：
     * 底层还是用到sqlitehelper获得greendao的helper,然后通过helper获取database,通过database创建DaoMaster
     * DaoMaster获取到DaoSession,通过DaoSession获取到想要的dao操作对象，例如UserDao
     */
    private void initGreenDAO() {
        //DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, DATABASE_NAME);
        DbOpenHelper helper = new DbOpenHelper(this, DATABASE_NAME);
        Database database = helper.getWritableDb();
        DaoMaster daoMaster = new DaoMaster(database);
       daoSession = daoMaster.newSession();
    }
}
